#include "qnywindow.h"
#include "ui_qnywindow.h"
#include <QSettings>
#include <QThread>
#include "common/deviceinfo.h"

QnyWindow::QnyWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QnyWindow),
    m_passive_netWorkHandler(Q_NULLPTR),
    m_qnyInfo(new DeviceInfo(this))
{
    ui->setupUi(this);

    //加载配置文件
    loadSettings();
}

QnyWindow::~QnyWindow()
{
    delete ui;
}

void QnyWindow::loadSettings()
{
    QSettings settings("config.ini", QSettings::IniFormat);
    settings.beginGroup("REMOTE_DESKTOP_SERVER");
    QString remoteHost = settings.value("remoteHost").toString();
    if(remoteHost.isEmpty())
    {
        remoteHost = "localhost";
        settings.setValue("remoteHost", remoteHost);
    }
    int remotePort = settings.value("remotePort", 0).toInt();
    if(0 == remotePort)
    {
        remotePort = 443;
        settings.setValue("remotePort", remotePort);
    }
    settings.endGroup();
    settings.sync();

    m_qnyInfo->setRemoteInfo(remoteHost, remotePort);
    //启动被控网络
    startPassiveConnect(m_qnyInfo);
}

void QnyWindow::startPassiveConnect(class DeviceInfo *Info)
{
    QThread *thread = new QThread(this);
    m_passive_netWorkHandler = new passivehandler;

    connect(thread, &QThread::started, m_passive_netWorkHandler, &passivehandler::createSocket);

    m_passive_netWorkHandler->moveToThread(thread);
    thread->start();
}
